﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using METODESA.Server.Data.METODESA.Entity;
using METODESA.Server.Data.METODESA.Nhibernate;
using System.Security.Cryptography;

namespace Metodesa.Logic
{
    public class EmpleadoLogic
    {

        public static IList<Empleado> processSearch(string search, string criteria)
        {
            if (criteria == "" || search == "")
            {
                return EmpleadoDao.getAllEmpleado();
            }
            else
            {

                switch (criteria)
                {
                    case "Ci": return EmpleadoDao.GetEmpleadoByCedula(search);

                    case "Name": return EmpleadoDao.GetEmpleadoByName(search);

                    case "LastName": return EmpleadoDao.GetEmpleadoByLastName(search);

                }

            }

            return null;
        }


        public static int update(Empleado modified,string login, string password)
        {
           
           
             if(!password.Equals(""))
             {
                 modified.Pass = EncodePassword(password);
             }
             if (!login.Equals(""))
             {
                 IList<Empleado> empleados = EmpleadoDao.GetEmpleadoByLogin(login);
                 if(empleados!=null && empleados.Count!=0)
                 {
                     return 1; // No se puede usar un nombre de usuario que ya esta asignado

                 }
             }

             try
             {
                 EmpleadoDao.updateEntity(modified);
             }catch(Exception e)
             {
                 return 2;
             }
            return 0;
        }

        public static int save (Empleado empleado)
        {
            
            try
            {
                IList<Empleado> empleados = EmpleadoDao.GetEmpleadoByLogin(empleado.Login);
                if (empleados != null && empleados.Count != 0) // ya existe un empleado con ese usuario
                    return 1;
                EmpleadoDao.saveEntity(empleado);
            }
            catch(Exception e)
            {
                return 2;
            }
            return 0;
        }

        public static Empleado getUser(string login,string password)
        {
            try
            {
                if (login.Equals("") || password.Equals(""))
                    return null;
                else
                    return EmpleadoDao.GetEmpleadoByUser(login, EncodePassword(password));
            }
            catch(Exception e)
            {
                
                return null;
            }
        }

        private static string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;
            //string a = originalPassword;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
             return BitConverter.ToString(encodedBytes);
            
        }
    }


}
